using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BBX.Common;
using BBX.Entity;
using BBX.Forum;
using NewLife.Web;

namespace BBX.Web
{
    public class onlineuser : PageBase
    {
        public List<Online> onlineuserlist;
        public Int32 onlineusernumber;
        public Int32 pageid = DNTRequest.GetInt("page", 1);
        public Int32 pagecount;
        public String pagenumbers = "";
        public Int32 totalonline;
        public Int32 totalonlineuser;
        public Int32 totalonlineguest;
        public Int32 totalonlineinvisibleuser;
        public String highestonlineusercount;
        public String highestonlineusertime;
        private Int32 startrow;
        private Int32 endrow;

        public String dir;

        protected override void ShowPage()
        {
            pagetitle = "在线列表";

            var order = Request["order"];
            if (String.IsNullOrEmpty(order)) order = WebHelper.ReadCookie("onlinelist", "order");
            dir = Request["dir"];
            if (String.IsNullOrEmpty(dir)) dir = WebHelper.ReadCookie("onlinelist", "dir");
            var desc = dir == "desc";

            onlineusernumber = onlineusercount;

            var pagesize = WebHelper.RequestInt("PageSize");
            if (pagesize <= 0) pagesize = 16;
            pagecount = ((onlineusernumber % pagesize == 0) ? (onlineusernumber / pagesize) : (onlineusernumber / pagesize + 1));
            pagecount = ((pagecount == 0) ? 1 : pagecount);
            if (pageid <= 1)
            {
                pageid = 1;
                startrow = 0;
                endrow = pagesize - 1;
            }
            else
            {
                pageid = ((pageid > pagecount) ? pagecount : pageid);
                startrow = (pageid - 1) * pagesize;
                endrow = pageid * pagesize;
            }

            startrow = ((startrow >= onlineusernumber) ? (onlineusernumber - 1) : startrow);
            endrow = ((endrow >= onlineusernumber) ? (onlineusernumber - 1) : endrow);
            //onlineuserlist = onlineuserlist.Skip(startrow).Take(endrow - startrow + 1).ToList();

            //onlineuserlist = Online.GetOnlineUserList(this.onlineusercount, order, desc, out this.totalonlineguest, out this.totalonlineuser, out this.totalonlineinvisibleuser);
            onlineuserlist = Online.GetList(0, order, desc, startrow, pagesize);

            var st = Online.GetStat();
            totalonline = st.Total;
            totalonlineuser = st.User;
            totalonlineinvisibleuser = st.Invisible;
            totalonlineguest = st.Guest;

            // 写入Cookie
            WebHelper.WriteCookie("onlinelist", "order", order);
            WebHelper.WriteCookie("onlinelist", "dir", dir);
            dir = desc ? "asc" : "desc";
            pagenumbers = ((String.IsNullOrEmpty(DNTRequest.GetString("search"))) ? Utils.GetPageNumbers(pageid, pagecount, "onlineuser.aspx", 8) : Utils.GetPageNumbers(pageid, pagecount, "onlineuser.aspx", 8));
            totalonline = onlineusercount;
            highestonlineusercount = Statistic.Current.HighestOnlineUserCount + "";
            highestonlineusertime = Statistic.Current.HighestOnlineUserTime.ToString("yyyy-MM-dd HH:mm:ss");
        }
    }
}